//
//  M_PaySucceedVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/6.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import MBProgressHUD

class M_PaySucceedVC: BaseViewControllerWithHeader {
    let likeView = M_LikeView()
    var orderData = [String:Any?]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    
    override func _onBackButtonClick() {
        self.popViewController(vCount: 3)
    }

    func getorderDetail(orderID: String, callback: @escaping ([String:Any?]) -> Void) {
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "",
                                    "orderID":orderID]
        API.request(url: .M_GetUserOrderDetail, params: params) { (result) in
            if result.success {
                if let data = result.data?.getDictionary("data") {
                    callback(data)
                }
            }
            else {
                let popup = PopupSureBox(title:"获取订单失败", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
                }
            }
        }
        
    }
}

// MARK: UI
extension M_PaySucceedVC {
    func setupUI() {
        safeView.backgroundColor = .xF2F2F2
        headView.backgroundColor = .xF2F2F2
        
        let img = scrollView.drawImage(named: "zhifuchenggong_115") { (make) in
            make.top.equalTo(20)
            make.centerX.equalToSuperview()
        }
        scrollView.drawLabel(text: "支付成功", fontSize: 18, textColor: .x333333) { (make) in
            make.top.equalTo(img.snp.bottom).offset(12)
            make.centerX.equalToSuperview()
        }
        
        var btn = scrollView.drawButton(title: "查看订单", titleColor: .x999999, fontSize: 14) { (make) in
            make.height.equalTo(35)
            make.width.equalTo(110)
            make.top.equalTo(img.snp.bottom).offset(50)
            make.centerX.equalToSuperview().offset(-60)
        }
        btn.addTarget(self, action: #selector(_chakanButtonClick(button:)), for: .touchUpInside)
        // 设置边框
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.x999999.cgColor
        btn.layer.cornerRadius = 17.5
        
        btn = scrollView.drawButton(title: "继续兑换", titleColor: .x999999, fontSize: 14) { (make) in
            make.height.equalTo(35)
            make.width.equalTo(110)
            make.top.equalTo(img.snp.bottom).offset(50)
            make.centerX.equalToSuperview().offset(60)
        }
        btn.addTarget(self, action: #selector(_jixuButtonClick(button:)), for: .touchUpInside)
        // 设置边框
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.x999999.cgColor
        btn.layer.cornerRadius = 17.5
        
        //画横线
        let line = scrollView.drawView { (make) in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(265)
            make.height.equalTo(0.5)
        }
        
        line.backgroundColor = .xF2F2F2
        
        likeView.getDataList(zoneCode: "105001", pageIndex: 1, pageSize: 10, callback: { (count) in
              var rownum = Double(count)/2
              rownum = ceil(rownum)
              self.scrollView.drawView(view: self.likeView) { (make) in
                    make.left.right.equalToSuperview()
                    make.top.equalTo(line.snp.bottom)
                    make.height.equalTo(rownum*260+52+20)
              }
              self.scrollView.bottomEqualTo(self.likeView, offset: 20)
        })
        likeView.callselBlock { (info) in
            let vc = M_ProductDetailVC(info: info)
            self.pushViewController(vc)
        }
    }
}

// MARK: event & action
extension M_PaySucceedVC {
    @objc func _chakanButtonClick(button: Button) {
//        let vc = M_OrderListVC(zoneCode:ZoneCodeModel(fromDictionary: ["name" : "全部订单",
//                                                                     "sort" : 2,
//                                                                     "code" : "100012"]))
//        pushViewController(vc)

        MBProgressHUD.showOrIncrease()
        self.getorderDetail(orderID: orderData.getString("orderID") ?? "", callback: { (orderDetail) in
            MBProgressHUD.hideOrDecrease()
            let vc = M_OrderDetail()
            vc.orderDic = orderDetail
            vc.backCount = 5
            self.pushViewController(vc)
        })
    }
    
    @objc func _jixuButtonClick(button: Button) {
        popViewController(vCount: 4)
    }
}
